﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CommonFunc;
using DAL = AccessDAL;
using DBUtility;

public partial class Manageweb_ManageProduct_ManageProduct : System.Web.UI.Page
{
    DAL.ProductClassTableAdapters.ProductClassTableAdapter DPC =
        new DAL.ProductClassTableAdapters.ProductClassTableAdapter();

    MODEL.Product MP = new MODEL.Product();
    MODEL.Product _MP = new MODEL.Product();
    MODEL.ProductClass MPC = new MODEL.ProductClass();
    MODEL.ProductClass _MPC = new MODEL.ProductClass();

    BLL.Product BP = new BLL.Product();
    BLL.ProductClass BPC = new BLL.ProductClass();


    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.Cookies["MasterID"] == null)
        {
            Response.Redirect("../Default.aspx");
        }

        if (!IsPostBack)
        {
            DDL_ClassDataBind();

            SessionInit();

            PageDataInit();
        }
    }


    #region 页面Session [初始化]
    protected void SessionInit()
    {
        try
        {
            if (Request.QueryString["ClassID"] == null)
            {
                Session.Remove("ProductClassCode");
            }
            else
            {
                MPC.PC_ID = Convert.ToInt32(Request.QueryString["ClassID"]);

                _MPC = BPC.GetDataByID(MPC);

                Session["ProductClassCode"] = _MPC.PC_ClassCode;
            }
        }
        catch (Exception ex)
        {
            LB_ManageProduct.Text = ex.Message;
        }
    }
    #endregion


    #region 页面数据 [初始化]
    protected void PageDataInit()
    {
        try
        {
            if (Session["ProductClassCode"] != null)
            {
                GV_ProductList.DataSourceID = "ODS_ProductList2";
                GV_ProductList.DataBind();
            }
            else
            {
                GV_ProductList.DataSourceID = "ODS_ProductList";
                GV_ProductList.DataBind();
            }
        }
        catch (Exception ex)
        {
            LB_ManageProduct.Text = ex.Message;
        }
    }
    #endregion


    #region DDL_Class数据绑定 [初始化]
    protected void DDL_ClassDataBind()
    {
        try
        {
            DDL_Class.Items.Clear();

            ListItem LI0 = new ListItem("", "");
            DDL_Class.Items.Add(LI0);

            DataTable DT = DPC.GetDataOrderByClassCode(false);

            foreach (DataRow dr in DT.Rows)
            {
                ListItem li = new ListItem(
                    ClassCode.CreateClassListItemText(dr["PC_Title"].ToString(), dr["PC_Level"].ToString()),
                    dr["PC_ID"].ToString());

                DDL_Class.Items.Add(li);
            }
        }
        catch (Exception ex)
        {
            LB_ManageProduct.Text = ex.Message;
        }
    }
    #endregion


    #region 类别标题 [格式化]
    protected string FormatClassTitle(object _Title, object _Level)
    {
        return ClassCode.CreateClassListItemText(_Title.ToString(), _Level.ToString());
    }
    #endregion


    #region 产品图片 [格式化]
    protected string FormatThumb(object _Thumb)
    {
        if (_Thumb.ToString() == "")
        {
            return "~/Manageweb/images/null.jpg";
        }
        else
        {
            return "~/UploadImage/" + _Thumb.ToString();
        }
    }
    #endregion


    #region 是否删除 [Title列] [格式化]
    protected bool FormatLB_IsDel(object _IsDel)
    {
        bool IsDel = Convert.ToBoolean(_IsDel);

        if (IsDel)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    #endregion


    #region 是否删除 [DelCommand按钮] [格式化]
    protected string FormatLBtn_Del(object _IsDel)
    {
        bool IsDel = Convert.ToBoolean(_IsDel);

        if (IsDel)
        {
            return "恢复";
        }
        else
        {
            return "删除";
        }
    }
    #endregion


    #region 是否首页推荐 [格式化]
    protected string FormatIndex(object _Index)
    {
        bool Index = Convert.ToBoolean(_Index);

        if (Index)
        {
            return "已推荐";
        }
        else
        {
            return "未推荐";
        }
    }
    #endregion


    #region 是否推荐 [Command] [GridView]
    protected void Btn_Index_Command(object sender, CommandEventArgs e)
    {
        try
        {
            MP.Product_ID = Convert.ToInt32(e.CommandArgument);

            BP.MarkIsIndex(MP);

            GV_ProductList.DataBind();
        }
        catch (Exception ex)
        {
            LB_ManageProduct.Text = ex.Message;
        }
    }
    #endregion


    #region 修改产品 [Command] [GridView]
    protected void LBtn_Mod_Command(object sender, CommandEventArgs e)
    {
        try
        {
            Response.Redirect("AddProduct.aspx?ProductID=" + e.CommandArgument.ToString());
        }
        catch (Exception ex)
        {
            LB_ManageProduct.Text = ex.Message;
        }
    }
    #endregion


    #region 删除产品 [Command] [GridView] (标记已删除)
    protected void LBtn_Del_Command(object sender, CommandEventArgs e)
    {
        try
        {
            MP.Product_ID = Convert.ToInt32(e.CommandArgument);

            BP.MarkIsDel(MP);

            LB_ManageProduct.Text = "操作成功";

            GV_ProductList.DataBind();
        }
        catch (Exception ex)
        {
            LB_ManageProduct.Text = ex.Message;
        }
    }
    #endregion


    #region 搜索按钮 [Click]
    protected void Btn_Search_Click(object sender, EventArgs e)
    {
        try
        {
            GV_ProductList.AllowPaging = false;

            GV_ProductList.DataSourceID = "ODS_Search";

            GV_ProductList.DataBind();
        }
        catch (Exception ex)
        {
            LB_ManageProduct.Text = ex.Message;
        }
    }
    #endregion


    #region 添加产品 [Click]
    protected void Btn_AddNews_Click(object sender, EventArgs e)
    {
        try
        {
            Response.Redirect("AddProduct.aspx?ClassID=" + DDL_Class.SelectedValue);
        }
        catch (Exception ex)
        {
            LB_ManageProduct.Text = ex.Message;
        }
    }
    #endregion


    #region 交换顺序按钮 [Click]
    protected void Btn_ConvertIndex_Click(object sender, EventArgs e)
    {
        try
        {
            if (DDL_Product1.SelectedValue == DDL_Product2.SelectedValue)
            {
                throw new Exception("两次所选产品相同");
            }

            AccHelper.ConvertValue("Product", "Product_Order", "Product_ID",
                DDL_Product1.SelectedValue, DDL_Product2.SelectedValue);

            LB_ManageProduct.Text = "交换成功";

            DDL_Class1.SelectedValue = "";
            DDL_Product1.SelectedValue = "";
            DDL_Class2.SelectedValue = "";
            DDL_Product2.SelectedValue = "";
            LB_Product1Index.Text = "";
            LB_Product2Index.Text = "";

            GV_ProductList.DataBind();
        }
        catch (Exception ex)
        {
            LB_ManageProduct.Text = ex.Message;
        }
    }
    #endregion


    #region DDL_Class1_SelectedIndexChanged
    protected void DDL_Class1_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            DDL_Product1.Items.Clear();

            ListItem li = new ListItem("", "");

            DDL_Product1.Items.Add(li);

            if (DDL_Class1.SelectedValue == "")
            {
                LB_Product1Index.Text = "";
            }
            else
            {
                DDL_Product1.DataBind();
            }

        }
        catch (Exception ex)
        {
            LB_ManageProduct.Text = ex.Message;
        }

    }
    #endregion


    #region DDL_Class2_SelectedIndexChanged
    protected void DDL_Class2_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            DDL_Product2.Items.Clear();

            ListItem li = new ListItem("", "");

            DDL_Product2.Items.Add(li);

            if (DDL_Class2.SelectedValue == "")
            {
                LB_Product2Index.Text = "";
            }
            else
            {
                DDL_Product2.DataBind();
            }
        }
        catch (Exception ex)
        {
            LB_ManageProduct.Text = ex.Message;
        }
    }
    #endregion


    #region DDL_Product1_SelectedIndexChanged
    protected void DDL_Product1_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            if (DDL_Product1.SelectedValue == "")
            {
                LB_Product1Index.Text = "";
            }
            else
            {
                MP.Product_ID = Convert.ToInt32(DDL_Product1.SelectedValue);

                _MP = BP.GetDataByID(MP);

                LB_Product1Index.Text = _MP.Product_Order.ToString();
            }

            
        }
        catch (Exception ex)
        {
            LB_ManageProduct.Text = ex.Message;
        }
    }
    #endregion


    #region DDL_Product2_SelectedIndexChanged
    protected void DDL_Product2_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            if (DDL_Product2.SelectedValue == "")
            {
                LB_Product2Index.Text = "";
            }
            else
            {
                MP.Product_ID = Convert.ToInt32(DDL_Product2.SelectedValue);

                _MP = BP.GetDataByID(MP);

                LB_Product2Index.Text = _MP.Product_Order.ToString();
            }
        }
        catch (Exception ex)
        {
            LB_ManageProduct.Text = ex.Message;
        }
    }
    #endregion


    #region 批量操作按钮 [Commend] [批量推荐] [取消推荐] [批量锁定] [取消锁定] [批量删除]
    protected void Btn_Multi_Command(object sender, CommandEventArgs e)
    {
        try
        {
            int ProductID;

            string SQLstr_Multi = "";

            switch (e.CommandName)
            {
                case "Multi_Index":
                    SQLstr_Multi = "UPDATE [Product] SET [Product_Index]=true ";
                    break;
                case "Multi_Non_Index":
                    SQLstr_Multi = "UPDATE [Product] SET [Product_Index]=false ";
                    break;
                case "Multi_Lock":
                    SQLstr_Multi = "UPDATE [Product] SET [Product_IsLock]=true ";
                    break;
                case "Multi_Non_Lock":
                    SQLstr_Multi = "UPDATE [Product] SET [Product_IsLock]=false ";
                    break;
                case "Multi_Del":
                    SQLstr_Multi = "UPDATE [Product] SET [Product_IsDel]=true ";
                    break;
            }

            foreach (GridViewRow GVR in GV_ProductList.Rows)
            {
                CheckBox CB = (CheckBox)GVR.FindControl("CB_Check");

                string SQLstr_Multi_2 = SQLstr_Multi;

                if (CB.Checked)
                {
                    int RowIndex = ((GridViewRow)CB.Parent.Parent).RowIndex;

                    ProductID = (int)GV_ProductList.DataKeys[RowIndex].Value;

                    SQLstr_Multi_2 += " WHERE [Product_ID]=" + ProductID.ToString();

                    AccHelper.ExecuteSQL(SQLstr_Multi_2);
                }
            }

            GV_ProductList.DataBind();
        }
        catch (Exception ex)
        {
            LB_ManageProduct.Text = ex.Message;
        }
    }
    #endregion


    #region 批量彻底删除 [Click]
    protected void Btn_Multi_RealDel_Click(object sender, EventArgs e)
    {
        try
        {
            foreach (GridViewRow GVR in GV_ProductList.Rows)
            {
                CheckBox CB = (CheckBox)GVR.FindControl("CB_Check");

                if (CB.Checked)
                {
                    int RowIndex = ((GridViewRow)CB.Parent.Parent).RowIndex;

                    MP.Product_ID = (int)GV_ProductList.DataKeys[RowIndex].Value;

                    BP.Del(MP);
                }
            }

            GV_ProductList.DataBind();
        }
        catch (Exception ex)
        {
            LB_ManageProduct.Text = ex.Message;
        }
    }
    #endregion
}
